package main

import (
	"fmt"

	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
)

var Db *gorm.DB

type Menu struct {
	gorm.Model
	Id   int    `db:"id"`
	Pid  int    `db:"pid"`
	Name string `db:"name"`
	Link string `db:"link"`
}

func init() {
	database, err := gorm.Open("mysql", "root:LongTao%1314@tcp(122.152.220.249:3306)/go_gin_api")
	// database, err := sqlx.Open("mysql", "root:123456@tcp(192.168.0.234:3306)/healthcloud")
	if err != nil {
		fmt.Println("open mysql faild", err)
		return
	}
	Db = database

}

func main() {
	var menu []Menu
	err := Db.First(&menu)
	defer Db.Close() // 查询后关闭db
	if err != nil {
		fmt.Println("exec faild ", err)
		return
	}
	fmt.Println("select result id: ", menu[0].Id)
}
